package org.batatasframework.support.database.sql.where;

import java.util.List;

import org.batatasframework.support.database.LogicSymbol;
import org.batatasframework.support.database.metadatas.FieldMetadata;
import org.batatasframework.support.database.sql.QueryParameter;
import org.batatasframework.support.database.sql.TableInstance;

import cn.bonoon.kernel.util.Opt;

public class SingleQueryFragment extends AbstractQueryFragment<Object>{

	public SingleQueryFragment(TableInstance tableSource, FieldMetadata fieldMetadata, Opt opt, Object value) {
		super(tableSource, fieldMetadata, opt, value);
	}

	@Override
	public void parse(LogicSymbol logic, StringBuilder sql, List<QueryParameter> paramenters) throws Exception {
		if(addParamenter(paramenters, value)){
			sql.append(logic.getValue());
			if(opt == Opt.NE || opt == Opt.NI){
				sql.append('(').append(columnName)
					.append(" is null or ").append(columnName)
					.append(Opt.NE.getOperator()).append(')');
				
				/*
				 * 不等于某个值，需要把null的情况处理一下，如：a<>10，理论上应该把所有非10的查出来
				 * 可以在实际查询的时间，那些a=null的是没有被查出来的
				 */
			}else{
				Opt opt = this.opt;
				if(opt == Opt.IN) opt = Opt.EQ;
				sql.append(columnName).append(opt.getOperator());
			}
		}
	}

}
